package dao.Impl;

import context.BaseContext;
import dao.BaseDao;
import dao.DishDao;
import model.DTO.DishEditDTO;
import model.DTO.DishPageDTO;
import model.VO.DishVO;
import model.entity.Dish;
import model.entity.*;
import util.NowTime;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class DishDaoImpl extends BaseDao implements DishDao {
    //修改菜品
    @Override
    public void edit(DishEditDTO d) {
        int flagD=0;
        int flagS=0;
        String sql="update dish set category_id=?,image=?,name=?,price=?,update_time=?,update_user=?";
       try {
            NowTime nowTime = new NowTime();
            String t = nowTime.getTime();
            int userId = BaseContext.getCurrentId();
            if(d.getDescription()!=null&&!d.getDescription().isEmpty()){
                sql+=",description=?";
                flagD+=1;
            }
            if(d.getStatus()!=null){
                sql+=",status=?";
                flagS+=1;
            }
            sql+=" where id=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,d.getCategoryId());
            pstmt.setString(2,d.getImage());
            pstmt.setString(3,d.getName());
            pstmt.setDouble(4,d.getPrice());
            pstmt.setTimestamp(5, Timestamp.valueOf(t));
            pstmt.setInt(6,userId);
//            pstmt.setString(7,d.getDescription());
//            pstmt.setInt(8,d.getStatus());
            if(flagD==0&&flagS==0){
                pstmt.setInt(7,d.getId());
            } else if (flagD==1&&flagS==0) {
                pstmt.setString(7,d.getDescription());
                pstmt.setInt(8,d.getId());
            } else if (flagD==0&&flagS==1) {
                pstmt.setInt(7,d.getStatus());
                pstmt.setInt(8,d.getId());
            } else {
                pstmt.setString(7,d.getDescription());
                pstmt.setInt(8,d.getStatus());
                pstmt.setInt(9,d.getId());
            }
            pstmt.executeUpdate();
            System.out.println("修改菜品成功");
        } catch (SQLException e) {
            System.out.println("修改菜品失败"+e);
        }
    }

    //批量删除菜品
    @Override
    public void del(String ids) {
        String[] delIds=ids.split(",");
        for (String delId : delIds) {
            String sql="delete from dish where id=?;";
            try {
                pstmt=conn.prepareStatement(sql);
                pstmt.setInt(1, Integer.parseInt(delId));
                pstmt.executeUpdate();
                System.out.println("批量删除菜品成功");
            } catch (SQLException e) {
                System.out.println("批量删除菜品失败"+e);
            }
        }
    }

    //新增菜品
    @Override
    public void add(int categoryId, String image, String name, double price, String description, int id, int status) {
        NowTime nowTime = new NowTime();
        String t = nowTime.getTime();
        int userId = BaseContext.getCurrentId();
        String sql="insert into dish (name, category_id, price, image, description, status, create_time,create_user,update_time,update_user) VALUES (?,?,?,?,?,?,?,?,?,?);";
        try{
            pstmt= conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setInt(2,categoryId);
            pstmt.setDouble(3,price);
            pstmt.setString(4,image);
            pstmt.setString(5,description);
            pstmt.setInt(6,status);
            pstmt.setTimestamp(7, Timestamp.valueOf(t));
            pstmt.setInt(8,userId);
            pstmt.setTimestamp(9, Timestamp.valueOf(t));
            pstmt.setInt(10,userId);
            pstmt.executeUpdate();
            System.out.println("新增菜品成功");
        } catch (SQLException e) {
            System.out.println("新增菜品失败"+e);
        }
    }

    //根据id查询
    @Override
    public DishVO find(int id) {
        DishVO d=new DishVO();
        String sql="select d.*,c.name from dish d left join category c on d.category_id=c.id where d.id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while(rs.next()){
                d.setCategoryId(rs.getInt("category_id"));
                d.setCategoryName(rs.getString("c.name"));
                d.setDescription(rs.getString("description"));
                d.setId(rs.getInt("id"));
                d.setImage(rs.getString("image"));
                d.setName(rs.getString("d.name"));
                d.setPrice(rs.getDouble("price"));
                d.setStatus(rs.getInt("status"));
                d.setUpdateTime(rs.getString("update_time"));
            }
            System.out.println(d);
            System.out.println("根据id查询成功");
        } catch (SQLException e) {
            System.out.println("根据id查询失败"+e);
        }
        return d;
    }

    //根据分类id查询
    @Override
    public List<Dish> getCateDishes(int categoryId) {
        List<Dish> dishList=new ArrayList<>();
        String sql="select * from dish where category_id=?;";
        try{
            pstmt= conn.prepareStatement(sql);
            pstmt.setInt(1,categoryId);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while(rs.next()){
                Dish d=new Dish();
                d.setCategory_id(rs.getInt("category_id"));
                d.setName(rs.getString("name"));
                d.setCreate_time(rs.getString("create_time"));
                d.setCreate_user(rs.getInt("create_user"));
                d.setDescription(rs.getString("description"));
                d.setId(rs.getInt("id"));
                d.setImage(rs.getString("image"));
                d.setPrice(rs.getFloat("price"));
                d.setStatus(rs.getInt("status"));
                d.setUpdate_time(rs.getString("update_time"));
                d.setUpdate_user(rs.getInt("update_user"));
                dishList.add(d);
            }
            System.out.println("根据分类id查询成功");
        } catch (SQLException e) {
            System.out.println("根据分类id查询失败"+e);
        }
        return dishList;
    }

    //分页查询
    @Override
    public List<DishVO> Page(DishPageDTO dishPageDTO) {
        List<DishVO> records=new ArrayList<>();
        String sql="select d.id,d.name,d.category_id,d.price,d.image,d.description,d.status,d.update_time,c.name from dish d left join category c on d.category_id=c.id where 1=1";
        if(dishPageDTO.getName()!=null && !dishPageDTO.getName().isEmpty()){
            sql+=" and d.name like '%"+dishPageDTO.getName()+"%'";
        }
        if(dishPageDTO.getCategoryId()!=null){
            sql+=" and c.id="+(int)dishPageDTO.getCategoryId();
        }
        if(dishPageDTO.getStatus()!=null){
            sql+=" and d.status="+(int)dishPageDTO.getStatus();
        }
        sql+=" limit ?,?";
        int page=dishPageDTO.getPage();
        int pageSize=dishPageDTO.getPageSize();
        page=(page-1)*pageSize;
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,page);
            pstmt.setInt(2,pageSize);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while(rs.next()){
                DishVO d=new DishVO();
                d.setId(rs.getInt("d.id"));
                d.setName(rs.getString("d.name"));
                d.setCategoryId(rs.getInt("category_id"));
                d.setPrice(rs.getDouble("price"));
                d.setImage(rs.getString("image"));
                d.setDescription(rs.getString("description"));
                d.setStatus(rs.getInt("d.status"));
                d.setUpdateTime(rs.getString("d.update_time"));
                d.setCategoryName(rs.getString("c.name"));
                System.out.println(d);
                records.add(d);
            }
            System.out.println("分页查询成功");
        } catch (SQLException e) {
            System.out.println("分页查询失败"+e);
        }
        return records;
    }
    //分页查询计数
    @Override
    public int count(DishPageDTO dishPageDTO) {
        int total=0;
        String sql="select * from dish d left join category c on d.category_id=c.id where 1=1";
        if(dishPageDTO.getName()!=null && !dishPageDTO.getName().isEmpty()){
            sql+=" and d.name like '%"+dishPageDTO.getName()+"%'";
        }
        if(dishPageDTO.getCategoryId()!=null){
            sql+=" and c.id="+(int)dishPageDTO.getCategoryId();
        }
        if(dishPageDTO.getStatus()!=null){
            sql+=" and d.status="+(int)dishPageDTO.getStatus();
        }
        sql+=" limit ?,?";
        int page=dishPageDTO.getPage();
        int pageSize=dishPageDTO.getPageSize();
        page=(page-1)*pageSize;
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,page);
            pstmt.setInt(2,pageSize);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while(rs.next()){
                total+=1;
            }
            System.out.println("分页查询计数成功");
        } catch (SQLException e) {
            System.out.println("分页查询计数失败"+e);
        }
        return total;
    }
    //修改菜品售卖状态
    @Override
    public void statusEdit(int id) {
        String sqlSelect="select status from dish where id=?;";
        String sqlUpdate="update dish set status=?,update_time=?,update_user=? where id=?;";
        try {
            NowTime nowTime = new NowTime();
            String t = nowTime.getTime();
            int userId = BaseContext.getCurrentId();
            int status = -1;

            pstmt=conn.prepareStatement(sqlSelect);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while(rs.next()){
                status=rs.getInt("status");
            }
            if(1==status){
                status=0;
                pstmt=conn.prepareStatement(sqlUpdate);
                pstmt.setInt(1,status);
                pstmt.setTimestamp(2, Timestamp.valueOf(t));
                pstmt.setInt(3,userId);
                pstmt.setInt(4,id);
                pstmt.executeUpdate();
            }
            else {
                status=1;
                pstmt=conn.prepareStatement(sqlUpdate);
                pstmt.setInt(1,status);
                pstmt.setTimestamp(2, Timestamp.valueOf(t));
                pstmt.setInt(3,userId);
                pstmt.setInt(4,id);
                pstmt.executeUpdate();
            }
            System.out.println("修改菜品售卖状态成功");
        } catch (SQLException e) {
            System.out.println("修改菜品售卖状态失败"+e);
        }
    }

    @Override
    public Dish getById(Integer dishId) {
         Dish dish = new Dish();
        String sql="select * from `dish` where id=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,dishId);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while (rs.next()){
                dish.setId(rs.getInt("id"));
                dish.setName(rs.getString("name"));
                dish.setDescription(rs.getString("description"));
                dish.setPrice(rs.getFloat("price"));
                dish.setImage(rs.getString("image"));
                dish.setStatus(rs.getInt("status"));
                dish.setCategory_id(rs.getInt("category_id"));
                dish.setCreate_time(String.valueOf(rs.getTime("create_time")));
                dish.setUpdate_time(String.valueOf(rs.getTime("update_time")));
                dish.setCreate_user(rs.getInt("create_user"));
                dish.setUpdate_user(rs.getInt("update_user"));
            }

        } catch (Exception e) {
            System.out.println("查询菜品失败："+e);
        }
        return dish;
    }

    @Override
    public List<Dish> list(String name) {
        List<Dish> dishList=new ArrayList<>();
        String sql="select * from `dish` where name like '%"+name+"%';";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while (rs.next()){
                Dish dish = new Dish();
                dish.setId(rs.getInt("id"));
                dish.setName(rs.getString("name"));
                dish.setDescription(rs.getString("description"));
                dish.setPrice(rs.getFloat("price"));
                dish.setImage(rs.getString("image"));
                dish.setStatus(rs.getInt("status"));
                dish.setCategory_id(rs.getInt("category_id"));
                dish.setCreate_time(String.valueOf(rs.getTime("create_time")));
                dish.setUpdate_time(String.valueOf(rs.getTime("update_time")));
                dish.setCreate_user(rs.getInt("create_user"));
                dish.setUpdate_user(rs.getInt("update_user"));
                dishList.add(dish);
            }
        } catch (Exception e) {
            System.out.println("模糊查询菜品失败："+e);
        }
        return dishList;
    }
}
